package top.conangao.common.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import top.conangao.common.holder.UserContextHolder;

import java.time.LocalDateTime;

/**
 * @author ConanGao
 * @description 自动填充功能
 * @since 2023/9/21 14:20
 */
public class CustomMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        UserContextHolder.User user = UserContextHolder.get();
        this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "createBy", user::getId, Long.class);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateBy", user::getId, Long.class);
        this.strictInsertFill(metaObject,"tenantId",user::getTenantId,Long.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        UserContextHolder.User user = UserContextHolder.get();
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateBy", user::getId, Long.class);
    }
}
